var BSTMG = BSTMG || {};
BSTMG.orderManager = {
	init: function(){
		var that = this;
		//分页功能
		that.showOrderList("","","");
		//订单状态查询
		that.orderState();
		//开通状态查询
		that.isDredge();
		///购买商品按钮查询
		that.query();
	},
	showOrderList: function(gName,orderState,isDredge){
		var url = "/sysAdmin/order/orderList/search?gName="+gName+"&orderState="+orderState+"&isDredge="+isDredge;
		BSTMG.common.hjhPages(url,function(data){
			var orderList = data.orderList;
            var $tbody = $("#orderList tbody"),
                html = '';
            if(orderList.length > 0) {
				for(var i=0;i<orderList.length;i++){
					var orderInfo = orderList[i];
					var payTime;
					if(orderInfo.payTime){
						payTime = new Date(orderInfo.payTime);
						payTime = toDate(payTime, "yyyy-MM-dd hh:mm:ss");
					}else{
						payTime = '';
					}
					var userEmailOrTell= orderInfo.userEmail;
					if(userEmailOrTell == null || userEmailOrTell == ''){
						userEmailOrTell = orderInfo.userTel;
					}
					var payMoney = setPayMoney(orderInfo);
					html += '<tr class="odd">';
					html += '<td>'+(i+1)+'</td>';
					html += '<td>'+BSTMG.tools.checkNull(orderInfo.orderNum)+'</td>';
					html += '<td>'+BSTMG.tools.checkNull(userEmailOrTell)+'</td>';
					html += '<td>'+BSTMG.tools.checkNull(orderInfo.belongCompanyName)+'</td>';
					html += '<td>'+BSTMG.tools.checkNull(orderInfo.companyDomainName)+'</td>';
					html += '<td>'+BSTMG.tools.checkNull(orderInfo.goodsName)+'</td>';
					html += '<td>'+BSTMG.tools.checkNull(parseFloat(payMoney).toFixed(2))+'</td>';
					html += '<td>'+BSTMG.tools.checkNull(payTime)+'</td>';
					html += '<td>'+setOrderState(orderInfo.orderState)+'</td>';
					html += '<td>'+setOrderType(orderInfo.isDredge)+'</td>';
					html += '<td><a class="btn btn-primary" href="/sysAdmin/order/orderDetail/'+orderInfo.orderId+'">详情</a>';
					if(orderInfo.orderState == 1 && orderInfo.buyType==0 && orderInfo.isDredge == 1)
						html += '<button type="button" class="btn btn-danger" onclick="toRefund('+orderInfo.orderId+');">退单</button>';
					html += '</td></tr>';
				}
				$tbody.html(html);
            } else {
                var $tr = '<tr class="odd"><td colspan="11">暂无数据！</td></tr>';
                $tbody.html($tr);
            }
		});
	},
	query : function(){
		$('#query').on('click',function(){
			var gName = $('#gName').val();
			BSTMG.orderManager.showOrderList(gName,"","");
		});
	},
	orderState : function(){
		$('#orderState').on('change',function(){
			var orderState = $('#orderState').val();
			var isDredge = $('#isDredge').val();
			BSTMG.orderManager.showOrderList("",orderState,isDredge);
		});
	},
	isDredge : function(){
		$('#isDredge').on('change',function(){
			var orderState = $('#orderState').val();
			var isDredge = $('#isDredge').val();
			BSTMG.orderManager.showOrderList("",orderState,isDredge);
		});
	},
}
//退单操作
function toRefund (orderId){
	BSTYUN.dialog.confirm("确定要退单吗？",function(){
		$.ajax({
			type : "POST",
			url : "/sysAdmin/operate/order/updateOrder",
			data:{
				orderId:orderId,
			},
			beforeSend: function(){
				var loading = function(html,callback){
			        var dialog = null;
			        dialog = $.dialog({
			            id: 'loading',
			            fixed:true,
			            title:false,
			            lock:true,
			            padding:'10px 20px',
			            content: '<div class="loading-tip">'+html+'</div>'
			        });
			    }
				loading('<span style="color:red;font-size:16px;">退单正在进行，时长大约1分钟！请耐心等待！<br>退单成功后，页面会自动刷新！</span>');
			},
			success:function(data){
				BSTYUN.dialog.close("loading");
				if(data.isSuccess) {
					BSTYUN.dialog.warn(data.message);
					BSTMG.orderManager.showOrderList("","","");
				}else {
					BSTYUN.dialog.warn(data.message);
				}
			},
			error : function() {
				BSTYUN.dialog.close("loading");
				BSTYUN.dialog.warn("网络错误，请稍后重试！");
			}
		})
	});
}
$(function(){
	BSTMG.orderManager.init();
});
//回车键查询功能
function keyQuery(){
	if(event.keyCode == 13){
		$('#query').trigger("click");
	}
}
//设置付款状态
function setOrderState(para){
	if (para == "0") {
		return "待付款";
	} else if (para == "1") {
		return "已付款";
	} else if(para == "2"){
		return "已作废";
	}
}
//设置交易金额
function setPayMoney(orderInfo){
	if(orderInfo.payMoney==0.00||orderInfo.payMoney==null||orderInfo.payMoney==""){
		return 0.00;
	}else{
		return orderInfo.payMoney;
	}
}
//设置开通状态
function setOrderType(para){
	if (para == "0") {
		return "未开通";
	} else if (para == "1") {
		return "已开通";
	}
}